package com.jluzh10.learning.service.Impl;

import com.jluzh10.learning.dao.CourseMapper;
import com.jluzh10.learning.dao.UserMapper;
import com.jluzh10.learning.dao.UsersCourseMapper;
import com.jluzh10.learning.domain.*;
import com.jluzh10.learning.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author yzg
 * @create 2019/5/20
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final CourseMapper courseMapper;
    private final UsersCourseMapper usersCourseMapper;
    private final BCryptPasswordEncoder passwordEncoder;

    @Autowired
    public UserServiceImpl(UserMapper userMapper, BCryptPasswordEncoder passwordEncoder,
                           UsersCourseMapper usersCourseMapper, CourseMapper courseMapper) {
        this.userMapper = userMapper;
        this.passwordEncoder = passwordEncoder;
        this.usersCourseMapper = usersCourseMapper;
        this.courseMapper = courseMapper;
    }


    @Override
    public List<User> findAll() throws Exception {
        return userMapper.selectByExample(null);
    }

    @Override
    public int addUser(User user) throws Exception {

        /*加密密码*/
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        //设置注册时间
        user.setCreateTime(new Date());
        //设置最后登录时间为注册时间
        user.setLoginTime(new Date());
        //设置默认昵称为用户名
        user.setNickname(user.getUsername());
        //保存用户
        return userMapper.insertSelective(user);
    }

    @Override
    public List<User> findByUsername(String username) throws Exception {
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(username);
        return userMapper.selectByExample(example);
    }

    @Override
    public List<User> findByMobile(String mobile) throws Exception {
        UserExample example = new UserExample();
        example.createCriteria().andMobileEqualTo(mobile);
        return userMapper.selectByExample(example);
    }

    @Override
    public boolean checkPassword(String password, String encodePassword) {
        return passwordEncoder.matches(password, encodePassword);
    }

    @Override
    public int update(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void collectCourse(Integer userId, Integer courseId,boolean isCollect) {
        UsersCourseExample example=new UsersCourseExample();
        example.createCriteria().andUserIdEqualTo(userId).andCourseIdEqualTo(courseId);
        //查询是否收藏
        List<UsersCourse> list = usersCourseMapper.selectByExample(example);
        Course course = courseMapper.selectByPrimaryKey(courseId);
        course.setPicture(null);
        if (list.size()>0&&!isCollect){
            //取消收藏
            usersCourseMapper.deleteByExample(example);
            //更新课程收藏人数
            course.setCollectionNum(course.getCollectionNum()-1);
            courseMapper.updateByPrimaryKeySelective(course);
        }else if (list.size()==0&&isCollect){
            //收藏课程
            usersCourseMapper.insert(new UsersCourse(userId,courseId));
            course.setCollectionNum(course.getCollectionNum()+1);
            courseMapper.updateByPrimaryKeySelective(course);
        }
    }

    @Override
    public List<Course> findByStudy(int userId, String orderBy) {
        return userMapper.findByStudy(userId, orderBy);
    }


}
